/*本模块功能:学生消息通知
        编写者:朱光曜
        编写时间:2017.11.20*/
$(function() {
    var Alert1 = new Alert;
    var confirm1 = new Confirm();
    function formatDate(now) {//处理时间戳
        var year = now.getFullYear();
        var month = now.getMonth() + 1;
        var date = now.getDate();
        var hour = now.getHours();
        var minute = now.getMinutes();
        if(month<10){month="0"+month;}
        if(date<10){date="0"+date;}
        if(hour<10){hour="0"+hour;}
        if(minute<10){minute="0"+minute;}
        return year + "-" + month + "-" + date;
    }
    var string1 = '<tr>' +
        '<th class="col-lg-9">消息通知</th>' +
        '<th class="col-lg-4">发布时间</th>' +
        // '<th class="col-lg-4">结束时间</th>' +
        '<th class="col-lg-3">发布人</th>' +

        '</tr>';

    function tabelModel(data) {//列表内容
        var time = new Date(data.noticeListByStu[key].releaseTime);
        var endTime=new Date(data.noticeListByStu[key].endtime);
        var noticeContent =  data.noticeListByStu[key].noticeContent.replace(/<br>/g,"\n");
        return '<tr >' +
            '<td class="col-lg-9" title="' + noticeContent+ '">' + noticeContent+ '</td>' +
            '<td class="col-lg-9" title="' + formatDate(time)+ '">' + formatDate(time)+ '</td>' +
            // '<td class="col-lg-9" title="' + formatDate(endTime)+ '">' + formatDate(endTime)+ '</td>' +
            '<td class="col-lg-2" title="' +data.noticeListByStu[key].userName+ '"><span>' +data.noticeListByStu[key].userName+ '</span></td>' +
            '</tr>';

    }
    var loading1 = new Loading();//监控全局ajax添加loading
    loading1.init();
    $(document).ajaxSend(function(){
        loading1.show();

    });
    $(document).ajaxStop(function(){
        loading1.hide();

    });
    var index = {
        init: function() {
            this.tab_content = {}; //列表集
            this.bind();
        },
        bind: function() {
            var self=this;
            this.parameter = {
                "pageNow": 1,
                "pageSize": 10
            };

            this.postData(this.parameter);

        },
        postData: function(par_data, nowData, hunt) { //获得搜索及列表数据
            var self = this;
            var parameter = par_data;

            $.ajax({
                type: "post",
                url: "getNoticeListByTeaId",
                async: true,
                data: {
                    "jsonStr": JSON.stringify(parameter)
                },
                success: function(data, status, xhr) {
                    var new_data = JSON.parse(data);
                    self.tab_content = new_data;
                    self.tab_add();

                    var sumpage = new_data.totalPage;
                    if(sumpage<1){sumpage=1;};
                    var nowpage = 1;
                    var pagenumber = 10;
                    var parame = parameter;
                    $(".tcdPageCode").createPage({
                        pageCount: sumpage, //总页数
                        current: nowpage, //当前页数
                        pageNumber: pagenumber, //每页显示数量
                        backFn: function(p) {

                            nowpage = p;

                            if(nowpage >= sumpage) {
                                nowpage = sumpage;
                            }
                            parameter.pageNow = nowpage;

                            $.ajax({
                                type: "post",
                                url: "getNoticeListByTeaId",
                                async: true,
                                data: {
                                    "jsonStr": JSON.stringify(parameter)
                                },
                                async: true,
                                success: function(msg) {
                                    //获取院系
                                    var msgNew = JSON.parse(msg);
                                    self.tab_content = msgNew;

                                    self.tab_add();
                                },
                                error: function(data, status, xhr) {
                                    var json = JSON.stringify({
                                        "pageName": "error"
                                    });
                                    window.location.href = "skipPage?jsonStr=" + json;

                                }
                            });
                        }
                    });
                },
                error: function(data, status, xhr) {
                    var json = JSON.stringify({
                        "pageName": "error"
                    });
                    window.location.href = "skipPage?jsonStr=" + json;

                }
            });

        },
        tab_add: function() { //列表追加

            var self = this;
            var data = self.tab_content;
            var table = string1;

            //data.selectList={};

            var i = 0;
            for(key in data.noticeListByStu) {
                i++;
            }

            if(i > 0) {

                for(key in data.noticeListByStu) {

                    table += tabelModel(data);
                }

                $(".mainContent .tableBox").html(table);


                //设置列表宽度(最大为9)
                var numMax = 0;
                $(".tableBox tr:first td").each(function() {
                    var str = $(this).attr("class");
                    var num = str.lastIndexOf("col-lg-") + 7;
                    var num1 = parseFloat(str.substring(num, num + 1));
                    numMax += num1;
                });

                $(".tableBox tr:first td").each(function() {
                    var str = $(this).attr("class");
                    var num = str.lastIndexOf("col-lg-") + 7;
                    var num1 = parseFloat(str.substring(num, num + 1));

                    $(this).css("width", num1 / numMax * 100 + "%");
                });
            } else {
                var trImg = "<tr><td colspan='6' style='border:none;'><img style='margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无消息</p></td/tr>";
                $(".mainContent .tableBox").html(string1+trImg);
            }


        },
        alert1: function(content1) { //alert提示

            Alert1.contenthead = "提示";
            Alert1.content = content1;

            Alert1.success = function() {
                window.location.reload();
                Alert1.hide();
            };

            Alert1.init();
            Alert1.show();
        }

    };
    index.init();
});